#include "ros/ros.h"
#include "turtlesim/Spawn.h"

int main(int argc, char** argv)
{
    ros::init(argc, argv, "turtle_spawn");

    ros::NodeHandle node;

    //请求"/spawn"服务，接到请求后创建一个客户端
    ros::service::waitForService("/spawn");     //阻塞型api，一直循环等待直到满足要求才往下执行
    //与名字叫“/spawn”的服务建立链接，用于向服务端“/spawn”发送请求
    ros::ServiceClient add_turtle = node.serviceClient<turtlesim::Spawn>("/spawn");

    //初始化turtlesim::spawn的请求数据
    turtlesim::Spawn srv;
    srv.request.x = 2.0;
    srv.request.y = 2.0;
    srv.request.name = "turtle2";

    ROS_INFO("Call service to spawn turtle[x:%0.6f, y:%0.6f, name:%s]", 
    srv.request.x, srv.request.y, srv.request.name.c_str());
    //发出服务请求
    add_turtle.call(srv);  //阻塞，把request发出去，等待response

    //显示服务调用结果
    ROS_INFO("Spawn turtle successfully [name: %s]", srv.response.name.c_str());

    return 0;
}
